﻿// Copyright 2012 Jack Bulan (agentgeo)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GemWorkshop.Formats.Sdat
{
    public static class SDATReader
    {
        public static SDAT Read(Stream s)
        {
            BinaryReader br = new BinaryReader(s);
            SDAT result = new SDAT();
            uint offset = (uint)br.BaseStream.Position;

            // Generic header
            result.id = br.ReadChars(4);
            br.BaseStream.Seek(4L, SeekOrigin.Current); // We don't need this!
            result.size = br.ReadUInt32();
            result.headerSize = br.ReadUInt16();
            result.sections = br.ReadUInt16(); // should be 4

            // SDAT header
            result.symbOffset = br.ReadUInt32();
            result.symbSize = br.ReadUInt32();
            result.infoOffset = br.ReadUInt32();
            result.infoSize = br.ReadUInt32();
            result.fatOffest = br.ReadUInt32();
            result.fatSize = br.ReadUInt32();
            result.fileOffest = br.ReadUInt32();
            result.fileSize = br.ReadUInt32();
            br.BaseStream.Seek(0x16L, SeekOrigin.Current); // reserved

            // Symbol or info stuff
            //char[] chars = br.ReadChars(4);
            //if (chars[0] == 0x53)
            //{
            //}
            // For now, assume everything's A-OK

            // Read the symbol section
            result.symb = new SDAT.SYMB();
            result.symb.id = br.ReadChars(4);
            result.symb.size = br.ReadUInt32();
            result.symb.sequencesOffest = br.ReadUInt32();
            result.symb.sfxOffset = br.ReadUInt32();
            result.symb.banksOffset = br.ReadUInt32();
            result.symb.wavesOffset = br.ReadUInt32();
            result.symb.playersOffset = br.ReadUInt32();
            result.symb.groupsOffset = br.ReadUInt32();
            result.symb.streamsOffset = br.ReadUInt32();
            // unknown
            br.BaseStream.Seek(4L, SeekOrigin.Current);
            // reserved
            br.BaseStream.Seek(0x24L, SeekOrigin.Current);
            result.symb.records = new SDAT.SYMB.Record[8]; // always 8 records
            for (int i = 0; i < 8; i++)
            {

            }

            return result;
        }
    }

    public struct SDAT
    {
        public char[] id;
        // ushort endianness, version
        public uint size;
        public ushort headerSize;
        public ushort sections;

        // Offset information
        public uint symbOffset, symbSize;
        public uint infoOffset, infoSize;
        public uint fatOffest, fatSize;
        public uint fileOffest, fileSize;

        public SYMB symb;
        public INFO info;
        public FAT fat;
        public FILE file;

        public struct SYMB
        {
            public char[] id;
            public uint size;
            public uint sequencesOffest, sfxOffset;
            public uint banksOffset, wavesOffset;
            public uint playersOffset, groupsOffset;
            public uint streamsOffset;
            // uint unknown;
            // padding - 0x24 bytes

            public Record[] records;

            public struct Record
            {
                public uint entries;
                public uint[] offsets;
                public SubRecord subRecord;

                public struct SubRecord
                {

                }
            }
        }

        public struct INFO
        {

        }

        public struct FAT
        {

        }

        public struct FILE
        {

        }
    }
}
